<template>
  <div class="infoList">
    <!-- 顶部搜索 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item>
        <el-button type="primary" @click="addstudents">新增</el-button>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item label="父亲">
        <el-input
          v-model="formInline.father"
          placeholder="学生父亲姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="母亲">
        <el-input
          v-model="formInline.mather"
          placeholder="学生母亲姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="datae" label="出生日期" align="center">
      </el-table-column>
      <el-table-column prop="idcard" label="身份证号" align="center">
      </el-table-column>
      <el-table-column prop="father" label="父亲" align="center">
      </el-table-column>
      <el-table-column prop="mother" label="母亲" align="center">
      </el-table-column>
      <el-table-column prop="address" label="家庭住址" align="center">
      </el-table-column>
      <el-table-column prop="time" label="入校时间" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="130px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="updataInfo(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="delData(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出表单 -->
    <el-dialog
      :title="state ? '添加学生信息' : '修改学生信息'"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model.number="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="出生日期"
          :label-width="formLabelWidth"
          prop="datae"
        >
          <el-input v-model="form.datae" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="身份证号"
          :label-width="formLabelWidth"
          prop="idcard"
        >
          <el-input v-model.number="form.idcard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父亲" :label-width="formLabelWidth" prop="father">
          <el-input v-model="form.father" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="母亲" :label-width="formLabelWidth" prop="mother">
          <el-input v-model="form.mother" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="家庭地址"
          :label-width="formLabelWidth"
          prop="address"
        >
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="入校时间"
          :label-width="formLabelWidth"
          prop="time"
        >
          <el-input v-model="form.time" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="联系方式"
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "InfoList",
  props: [""],
  data() {
    return {
      tableData: [],
      //弹出表单标题
      state: true,
      dialogFormVisible: false,
      formInline: {
        name: "",
        mother: "",
        father: "",
      },
      //表单信息
      formLabelWidth: "80px",
      form: {
        name: "",
        sex: "1",
        datae: "1225",
        address: "山西省临汾市",
        phone: "",
        age: "",
        idcard: "1426",
        mother: "",
        father: "",
        time: "",
      },
      // 表单验证规则
      rules: {
        name: [{ required: true, message: "请输入姓名" }],
        sex: [{ required: true }],
        age: [
          { required: true, message: "请输入年龄" },
          { type: "number", message: "年龄必须是数字" },
        ],
        datae: [{ required: true, message: "出生日期" }],
        idcard: [{ required: true, message: "请输入身份证号" }],
        // father: [{ required: true}],
        // mother: [{ required: true}],
        time: [{ required: true, message: "请输入入学时间" }],
        address: [{ required: true, message: "请输入您的住址" }],
        phone: [{ required: true, message: "请输入联系方式" }],
      },
    };
  },

  components: {},

  computed: {},

  created() {
    this.getData();
  },

  mounted() {},

  methods: {
    getData() {
      this.servers
        .get("/msg")
        .then((res) => {
          // console.log(res);
          if (res.status == 200) {
            this.tableData = [res.data.data];
          }
        })
        .catch((err) => {
          console.log(res);
        });
    },
    //查询的方法
    find() {
      console.log(this.formInline);
    },
    //修改数据的方法
    updataInfo(row) {
      console.log(row);
      //显示弹窗状态
      this.state = false;
      this.dialogFormVisible = true;
      this.form = row;
    },
    //删除数据的方法
    delData(row) {
      console.log(row);
    },
    //新增方法
    addstudents() {
      this.state = true;
      this.dialogFormVisible = true;
    },
    sure(form) {
      // console.log(this.form);
      this.$refs[form].validate((result) => {
        if (result) {
          //通过调用新增信息接口
          this.tableData.push(this.form);
          this.dialogFormVisible = false;
          this.form = {};
          if (this.state) {
            //如果是ture调用添加接口
          } else {
            //调用修改的接口
          }
        } else {
          console.error(this.form);
        }
      });
    },
  },
};
</script>
<style scoped lang='less'>
.infoList {
  .el-select {
    width: 100%;
  }
  .dialog-footer {
    text-align: center;
  }
}
</style>